const app = getApp()
const Api = require("../../../utils/api.js")
const ApiRequest = require("../../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: "",
    loginUser: null,
    modalName: "",
    name: null,
    idCard: null,
    phoneNum: null,
    age: null,
    gender: null,
    btnText: "确定",
    flag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let userInfo = app.globalData.userInfo;
    let loginUser = app.globalData.loginUser;
    this.setData({
      avatarUrl: userInfo.avatarUrl,
      loginUser: loginUser,
      name: loginUser.realName,
      idCard: loginUser.idCard,
      phoneNum: loginUser.phoneNum,
      age: loginUser.age,
      gender: loginUser.gender
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 查询挂号
   */
  getFlow: function(e) {
    var self = this
    let state = e.currentTarget.dataset.state
    let patientID = app.globalData.loginUser.id
    var url = Api.find_flow_patient
    var params = {
      patientId: patientID,
      state: state
    };
    var method = 'GET';
    var header = "application/json";
    if (state == 1) { //当前挂号
      var success = function(res) {
        let data = res.data
        if (res.code == 200 && data.length != 0) {
          wx.navigateTo({
            url: '../list/list?flowList=' + JSON.stringify(data) + '&state=' + state,
          })
        } else if (res.code == 200 && data.length == 0) {
          wx.showToast({
            icon: 'none',
            title: '您当前没有挂号哦',
          })
        }
      }
    } else if (state == -1) { //历史挂号
      var success = function(res) {
        let data = res.data
        console.log("state == -1", res)
        if (res.code == 200 && data.length != 0) {
          wx.navigateTo({
            url: '../list/list?flowList=' + JSON.stringify(data)
          })
        } else if (res.code == 200 && data.length == 0) {
          wx.showToast({
            icon: 'none',
            title: '您当前没有历史挂号哦',
          })
        }
      }
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  fail: function(e) {
    console.log("请求失败：", e)
  },
  /**
   * 扫描二维码
   */
  scanCode: function() {
    var self = this
    // 只允许从相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log("扫描返回对象", res)
        let patientId = app.globalData.loginUser.id
        let doctorId = res.result
        wx.showLoading({
          title: '请稍后',
        })
        self.newFlow(patientId, doctorId)
      }
    })
  },
  /**
   * 新建医患关系
   */
  newFlow: function(patientId, doctorId) {
    var self = this
    var url = Api.newFlow
    var params = {
      patientId: patientId,
      doctorId: doctorId
    };
    console.log("新建医患关系 params: ", params)

    var method = 'POST';
    var header = "application/x-www-form-urlencoded";
    var success = function(res) {
      console.log("新建医患关系res: ", res)
      if (res.code == 200) {
        let data = res.data
        wx.showModal({
          title: '提示',
          content: '成功建立医患关系！',
        })
      } else {
        wx.showModal({
          title: '错误',
          content: res.msg,
        })
      }
      wx.hideLoading()
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  /**
   * 展示用户基本信息
   */
  showInfo: function(e) {
    let self = this
    self.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideInfo: function(e) {
    let flag = e.currentTarget.dataset.target
    if (flag) {
      // 修改资料
      this.updatePatient()
    } else {
      this.setData({
        modalName: null
      })
    }
  },
  updatePatient: function(e) {
    var self = this
    let loginUser = self.data.loginUser
    var url = Api.patient_updata
    var params = {
      openid: loginUser.openId,
      realName: self.data.name,
      idCard: self.data.idCard,
      phoneNum: self.data.phoneNum,
      age: self.data.age,
      gender: self.data.gender
    };
    var method = 'POST';
    var header = "application/json";
    var success = function(res) {
      let data = res.data
      app.globalData.loginUser = data
      if (res.code == 200) {
        wx.showToast({
          title: '修改成功',
        })
        console.log("修改信息成功res: ", res)
        self.setData({
          modalName: null,
          flag: false,
          btnText: "确定",
          loginUser: data
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '选择值不能为空！',
        })
      }
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      flag: true,
      gender: e.detail.value
    })
  },
  getName: function(e) {
    this.setData({
      name: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
  getIDCard: function(e) {
    this.setData({
      idCard: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
  getPhoneNum: function(e) {
    this.setData({
      phoneNum: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
  getAge: function(e) {
    this.setData({
      age: e.detail.value,
      flag: true,
      btnText: "修改"
    })
  },
})